library(pacman)
## Warning: package 'pacman' was built under R version 4.3.3
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.3
## Warning: package 'ggplot2' was built under R version 4.3.3
## Warning: package 'tidyr' was built under R version 4.3.3
## Warning: package 'readr' was built under R version 4.3.3
## Warning: package 'purrr' was built under R version 4.3.3
## Warning: package 'dplyr' was built under R version 4.3.3
## Warning: package 'stringr' was built under R version 4.3.3
## Warning: package 'forcats' was built under R version 4.3.3
## Warning: package 'lubridate' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(plotly)
## Warning: package 'plotly' was built under R version 4.3.3
##
## Attaching package: 'plotly'
##
## The following object is masked from 'package:ggplot2':
##
## last_plot
##
## The following object is masked from 'package:stats':
##
## filter
##
## The following object is masked from 'package:graphics':
##
## layout
library(DT)
## Warning: package 'DT' was built under R version 4.3.3
A Simple World Map
map <- map_data("world")
ggplot(map, aes(x=long, y=lat, group=group))+geom_polygon(fill="lightblue", color="white")+theme_void()

A Map for Specific Regions
North_Asia <- c("China", "Japan", "Mongolia", "North Korea", "South Korea", "Taiwan")
North_Asia_map <- map_data("world", region= North_Asia)
region.data <- North_Asia_map %>% group_by(region) %>% summarize(long=mean(long), lat=mean(lat)) %>% arrange()
ggplot(North_Asia_map, aes(x=long, y=lat))+geom_polygon(aes(group=group, fill=region))+geom_text(data=region.data, aes(label=region), size=5, hjust=0.5, col="#808080", fontface="bold")+scale_fill_viridis_d()+theme_void()+theme(legend.position="none")

A Chloropleth Map
drinks <- read.csv('./drinks.csv')
drinks_map <- drinks %>% left_join(map, by =c("country"="region"))
ggplot(drinks_map, aes(long, lat, group=group))+geom_polygon(aes(fill=total_litres_of_pure_alcohol),color="white")+scale_fill_viridis_c(option="C")+labs(fill="Total Litres of Pure Alcohol")+theme_void()+theme(legend.position="bottom")

ggplot(drinks, aes(map_id=country))+geom_map(aes(fill=total_litres_of_pure_alcohol), map= map, color="white")+expand_limits(x=map$long, y=map$lat)+labs(fill="Total Litres of Pure Alcohol")+theme_void()+theme(legend.position="bottom")

ggplot(drinks, aes(map_id=country))+geom_map(aes(fill=total_litres_of_pure_alcohol), map=map, color="white")+expand_limits(x=map$long, y=map$lat)+labs(fill="Total Litres of Pure Alcohol")+theme_void()+theme(legend.position="bottom")

A U.S. State-Level Map
US_map <- map_data("state")
state_data<- US_map %>% filter(region != "district of columbia") %>% group_by(region) %>% summarize(long=mean(long), lat=mean(lat)) %>% arrange(region)
state_data$region.abb <- state.abb[-c(2,11)] #drop Alaska and Hawai'i
p<- ggplot(US_map, aes(x=long, y=lat))+geom_polygon(aes(group=group,fill=region), color="white")+geom_text(data=state_data, aes(label=region.abb), fontface="bold")+theme_void()+theme(legend.position="none")
p

A Dynamic Map using plotly - 1
library(plotly)
ggplotly(p)
A Dynamic Map using plotly - 2
crimes <- data.frame(region=rownames(USArrests), USArrests) %>% filter(region!=c("Alaska", "Hawaii"))
crimes$region<- tolower(crimes$region)
crimes_map <- crimes %>% left_join(US_map, by="region")
g1<- ggplot(crimes_map, aes(x=long, y=lat))+
geom_polygon(aes(group=group, fill=Murder,
text = paste0(region, ":\n",
Murder, "murder arrests per 100,000"),
color="white"))+
geom_text(data=state_data,
aes(label=region.abb), fontface="bold", size=3)+
scale_fill_viridis_c(option="C")+theme_void()
## Warning in geom_polygon(aes(group = group, fill = Murder, text = paste0(region,
## : Ignoring unknown aesthetics: text
ggplotly(g1, tooltip="text")
A County-Level Map
covid <- read_csv("./COVID19.csv")
## Rows: 1175665 Columns: 30
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (4): id, administrative_area_level_1, administrative_area_level_2, adm...
## dbl (25): confirmed, deaths, people_vaccinated, people_fully_vaccinated, sc...
## date (1): date
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
ohio <- covid %>% filter(administrative_area_level_2=="Ohio", date=="2021-12-31")
ohio <- ohio %>% rename(county=administrative_area_level_3)
ohio_county <- map_data("county", region= "ohio")
ohio_county$subregion <- str_to_title(ohio_county$subregion)
ohio_map <- ohio %>% left_join(ohio_county, by=c("county"="subregion"))
g2 <- ggplot(ohio_map,
aes(x= long, y= lat))+
geom_polygon(aes(group=group, fill=deaths,
text=paste0("County: ", county, "\n",
"Total Deaths: ", deaths)))+
geom_text(data=ohio, aes(x= longitude, y=latitude, label=county),
color="white", fontface="bold")+
scale_fill_viridis_c(option="H")+
theme_minimal()+theme_void()
## Warning in geom_polygon(aes(group = group, fill = deaths, text =
## paste0("County: ", : Ignoring unknown aesthetics: text
font <- list(family="Arial", size=15, color="white")
label <- list(bgcolor="#232F34", bordercolor="transparent", font=font)
ggplotly(g2, tooltip="text", width=850, height=800) %>% style(hoverlabel=label) %>% layout(font=font)